/*
	File:		Entity.cpp
	Course:		SGD 1303
	Author:		dmonroe
	Purpose:	CEntity class is the base class for game entities
*/

#include "Entity.h"

#include "../SGD Wrappers/CSGD_TextureManager.h"

#include <cassert>


//**************************************************
// CONSTRUCTOR
CEntity::CEntity(void)
{
	// Set the entity type!
	m_eType			= ENT_BASE;


	// Set the data members
	m_fPosX			= 0;
	m_fPosY			= 0;
	
	m_fVelX			= 0;
	m_fVelY			= 0;
	
	m_nImageID		= -1;

	m_nWidth		= 1;
	m_nHeight		= 1;

	// Calling 'new' gives the first reference
	m_unRefCount	= 1;
}


//**************************************************
// DESTRUCTOR
CEntity::~CEntity(void)
{
}


//**************************************************
// Update
//	- move the entity based on its velocity
/*virtual*/	void CEntity::Update( float fElapsedTime )
{
	// velocity is the rate of change per second
	m_fPosX += m_fVelX * fElapsedTime;
	m_fPosY += m_fVelY * fElapsedTime;
}


//**************************************************
// Render
//	- render the entity's image at its position
/*virtual*/ void CEntity::Render( void )
{
	// Validate the image
	assert( m_nImageID != -1 && "CEntity::Render - image was not set");


	// Render the image
	CSGD_TextureManager::GetInstance()->Draw(
		m_nImageID, (int)m_fPosX, (int)m_fPosY );
	
}


//**************************************************
// GetRect
//	- returns the bounding box
/*virtual*/ RECT CEntity::GetRect( void ) const
{
	RECT rBoundingBox = 
	{
		(LONG)GetPosX(),					// left
		(LONG)GetPosY(),					// top
		(LONG)(GetPosX() + GetWidth()),		// right
		(LONG)(GetPosY() + GetHeight())		// bottom
	};

	return rBoundingBox;
}


//**************************************************
// CheckCollision
//	- checks for rect-to-rect collision with the other entity
/*virtual*/ bool CEntity::CheckCollision( IEntity* pOther )
{
	RECT rSelf	= this->GetRect();
	RECT rOther	= pOther->GetRect();
	RECT rOverlap;

	if( IntersectRect( &rOverlap, &rSelf, &rOther ) == FALSE )
		return false;
	return true;
}


//**************************************************
// AddRef
//	- increase the reference count
/*virtual*/ void CEntity::AddRef( void )
{
	assert( m_unRefCount != UINT_MAX && "CEntity::AddRef - reference limit reached" );

	++m_unRefCount;
}

//**************************************************
// Release
//	- decrease the reference count
//	- self-destruct when there are no more references
/*virtual*/ void CEntity::Release( void )
{
	--m_unRefCount;

	if( m_unRefCount == 0 )
		delete this;
}
